/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.Comparator;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ReSortingQueryResults
/*     */   implements ZimbraQueryResults
/*     */ {
/*     */   private static final int MAX_BUFFERED_HITS = 10000;
/*     */   private final ZimbraQueryResults results;
/*     */   private final SortBy sort;
/*  37 */   private List<ZimbraHit> mHitBuffer = null;
/*  38 */   private int iterOffset = 0;
/*     */   private final SearchParams params;
/*     */   
/*     */   public ReSortingQueryResults(ZimbraQueryResults results, SortBy sort, SearchParams params) {
/*  42 */     this.results = results;
/*  43 */     this.sort = sort;
/*  44 */     this.params = params;
/*     */   }
/*     */   
/*     */   public long getCursorOffset()
/*     */   {
/*  49 */     return -1L;
/*     */   }
/*     */   
/*     */   public void close() throws IOException
/*     */   {
/*  54 */     this.results.close();
/*     */   }
/*     */   
/*     */   public ZimbraHit getNext() throws ServiceException
/*     */   {
/*  59 */     if (hasNext()) {
/*  60 */       ZimbraHit toRet = peekNext();
/*  61 */       this.iterOffset += 1;
/*  62 */       return toRet;
/*     */     }
/*  64 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */   public List<QueryInfo> getResultInfo()
/*     */   {
/*  70 */     return this.results.getResultInfo();
/*     */   }
/*     */   
/*     */   public SortBy getSortBy()
/*     */   {
/*  75 */     return this.sort;
/*     */   }
/*     */   
/*     */   public boolean hasNext() throws ServiceException
/*     */   {
/*  80 */     return this.iterOffset < getHitBuffer().size();
/*     */   }
/*     */   
/*     */   public ZimbraHit peekNext() throws ServiceException
/*     */   {
/*  85 */     List<ZimbraHit> buffer = getHitBuffer();
/*  86 */     if (hasNext()) {
/*  87 */       return (ZimbraHit)buffer.get(this.iterOffset);
/*     */     }
/*  89 */     return null;
/*     */   }
/*     */   
/*     */   public void resetIterator()
/*     */     throws ServiceException
/*     */   {
/*  95 */     this.iterOffset = 0;
/*     */   }
/*     */   
/*     */   public ZimbraHit skipToHit(int hitNo) throws ServiceException
/*     */   {
/* 100 */     List<ZimbraHit> buffer = getHitBuffer();
/* 101 */     if (hitNo >= buffer.size()) {
/* 102 */       this.iterOffset = buffer.size();
/*     */     } else {
/* 104 */       this.iterOffset = hitNo;
/*     */     }
/* 106 */     return getNext();
/*     */   }
/*     */   
/*     */   public boolean isPreSorted()
/*     */   {
/* 111 */     return this.results.isPreSorted();
/*     */   }
/*     */   
/*     */   private List<ZimbraHit> getHitBuffer() throws ServiceException {
/* 115 */     if (this.mHitBuffer == null) {
/* 116 */       bufferAllHits();
/*     */     }
/* 118 */     return this.mHitBuffer;
/*     */   }
/*     */   
/*     */   private boolean isTaskSort() {
/* 122 */     switch (this.sort) {
/*     */     case TASK_DUE_ASC: 
/*     */     case TASK_DUE_DESC: 
/*     */     case TASK_STATUS_ASC: 
/*     */     case TASK_STATUS_DESC: 
/*     */     case TASK_PERCENT_COMPLETE_ASC: 
/*     */     case TASK_PERCENT_COMPLETE_DESC: 
/* 129 */       return true;
/*     */     }
/* 131 */     return false;
/*     */   }
/*     */   
/*     */   private void bufferAllHits() throws ServiceException
/*     */   {
/* 136 */     assert (this.mHitBuffer == null);
/* 137 */     this.mHitBuffer = new ArrayList();
/*     */     
/*     */     Comparator<ZimbraHit> comp;
/*     */     
/* 141 */     switch (this.sort) {
/*     */     case TASK_DUE_ASC: 
/*     */     default: 
/* 144 */       comp = new Comparator()
/*     */       {
/*     */         public int compare(ZimbraHit lhs, ZimbraHit rhs) {
/* 147 */           return TaskHit.compareByDueDate(true, lhs, rhs);
/*     */         }
/* 149 */       };
/* 150 */       break;
/*     */     case TASK_DUE_DESC: 
/* 152 */       comp = new Comparator()
/*     */       {
/*     */         public int compare(ZimbraHit lhs, ZimbraHit rhs) {
/* 155 */           return TaskHit.compareByDueDate(false, lhs, rhs);
/*     */         }
/* 157 */       };
/* 158 */       break;
/*     */     case TASK_STATUS_ASC: 
/* 160 */       comp = new Comparator()
/*     */       {
/*     */         public int compare(ZimbraHit lhs, ZimbraHit rhs) {
/* 163 */           return TaskHit.compareByStatus(true, lhs, rhs);
/*     */         }
/* 165 */       };
/* 166 */       break;
/*     */     case TASK_STATUS_DESC: 
/* 168 */       comp = new Comparator()
/*     */       {
/*     */         public int compare(ZimbraHit lhs, ZimbraHit rhs) {
/* 171 */           return TaskHit.compareByStatus(false, lhs, rhs);
/*     */         }
/* 173 */       };
/* 174 */       break;
/*     */     case TASK_PERCENT_COMPLETE_ASC: 
/* 176 */       comp = new Comparator()
/*     */       {
/*     */         public int compare(ZimbraHit lhs, ZimbraHit rhs) {
/* 179 */           return TaskHit.compareByCompletionPercent(true, lhs, rhs);
/*     */         }
/* 181 */       };
/* 182 */       break;
/*     */     case TASK_PERCENT_COMPLETE_DESC: 
/* 184 */       comp = new Comparator()
/*     */       {
/*     */         public int compare(ZimbraHit lhs, ZimbraHit rhs) {
/* 187 */           return TaskHit.compareByCompletionPercent(false, lhs, rhs);
/*     */         }
/* 189 */       };
/* 190 */       break;
/*     */     case NAME_LOCALIZED_ASC: 
/*     */     case NAME_LOCALIZED_DESC: 
/* 193 */       comp = this.sort.getHitComparator(this.params.getLocale());
/*     */     }
/*     */     
/*     */     
/* 197 */     int maxIfPresorted = 10000;
/* 198 */     if ((this.params != null) && (this.params.getCursor() == null)) {
/* 199 */       maxIfPresorted = this.params.getLimit();
/* 200 */       if (maxIfPresorted > 0)
/*     */       {
/* 202 */         maxIfPresorted = maxIfPresorted + 1 + this.params.getOffset();
/*     */       }
/*     */     }
/*     */     ZimbraHit cur;
/* 206 */     while ((cur = this.results.getNext()) != null)
/*     */     {
/* 208 */       if ((isTaskSort()) && 
/* 209 */         (!(cur instanceof TaskHit)) && (!(cur instanceof ProxiedHit))) {
/* 210 */         throw ServiceException.FAILURE("Invalid hit type, can only task-sort Tasks", null);
/*     */       }
/*     */       
/*     */ 
/* 214 */       boolean skipHit = false;
/*     */       
/* 216 */       boolean handleCursorFilteringForFirstHit = true;
/* 217 */       if (DebugConfig.enableContactLocalizedSort) {
/* 218 */         switch (this.sort) {
/*     */         case NAME_LOCALIZED_ASC: 
/*     */         case NAME_LOCALIZED_DESC: 
/* 221 */           handleCursorFilteringForFirstHit = false;
/*     */         }
/*     */         
/*     */       }
/*     */       
/*     */ 
/* 227 */       if ((this.params != null) && (this.params.getCursor() != null)) {
/* 228 */         ZimbraHit firstHit = null;
/* 229 */         if (this.params.getCursor().getSortValue() != null) {
/* 230 */           firstHit = new ResultsPager.CursorHit(this.results, this.params.getCursor().getSortValue(), this.params.getCursor().getItemId().getId());
/*     */         }
/*     */         
/* 233 */         ZimbraHit endHit = null;
/* 234 */         if (this.params.getCursor().getEndSortValue() != null) {
/* 235 */           endHit = new ResultsPager.CursorHit(this.results, this.params.getCursor().getEndSortValue(), 0);
/*     */         }
/*     */         
/* 238 */         if ((handleCursorFilteringForFirstHit) && 
/* 239 */           (firstHit != null) && (comp.compare(cur, firstHit) < 0)) {
/* 240 */           skipHit = true;
/*     */         }
/*     */         
/* 243 */         if ((endHit != null) && (comp.compare(cur, endHit) >= 0)) {
/* 244 */           skipHit = true;
/*     */         }
/*     */       }
/*     */       
/* 248 */       if (!skipHit) {
/* 249 */         this.mHitBuffer.add(cur);
/*     */       }
/* 251 */       if (this.mHitBuffer.size() >= 10000) {
/*     */         break;
/*     */       }
/*     */       
/* 255 */       if ((this.results.isPreSorted()) && (this.mHitBuffer.size() >= maxIfPresorted)) {
/*     */         break;
/*     */       }
/*     */     }
/*     */     
/* 260 */     if (!this.results.isPreSorted()) {
/* 261 */       Collections.sort(this.mHitBuffer, comp);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/ReSortingQueryResults.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */